package com.mp.chart.renderer;

import android.graphics.Canvas;
import android.text.TextUtils;

import com.mp.chart.components.AJYAxis;
import com.mp.chart.components.YAxis;
import com.mp.chart.utils.Transformer;
import com.mp.chart.utils.Utils;
import com.mp.chart.utils.ViewPortHandler;

public class AJYAxisRenderer extends YAxisRenderer {

    protected AJYAxis mYAxis;

    public AJYAxisRenderer(ViewPortHandler viewPortHandler, AJYAxis yAxis, Transformer trans) {
        super(viewPortHandler, yAxis, trans);
        mYAxis = yAxis;
    }


    protected void computeAxisValues(float min, float max) {
        if (mYAxis.isShowOnlyMinMaxEnabled()) {
            mYAxis.mEntryCount = 2;
            mYAxis.mEntries = new float[2];
            mYAxis.mEntries[0] = min;
            mYAxis.mEntries[1] = max;
            return;
        }
        float base = mYAxis.getBaseValue();

        if (mYAxis.isShowBaseValueAndMaxmin()) {

            mYAxis.mEntryCount = 3;
            mYAxis.mEntries = new float[3];


            float a = Math.max(Math.abs(max), Math.abs(min));

            mYAxis.mEntries[0] = base == 0 ? a : max;
            mYAxis.mEntries[1] = mYAxis.getBaseValue();
            mYAxis.mEntries[2] = base == 0 ? -a : min;


            return;

        }

        if (Float.isNaN(mYAxis.getBaseValue())) {
            super.computeAxisValues(min, max);

            return;
        }
        /*与下面重合,待优化*/
        float yMin = min;
        float yMax = max;

        int labelCount = mYAxis.getLabelCount();

        float interval = (yMax - yMin) / (labelCount - 1);
        int n = labelCount;
        mYAxis.mEntryCount = n;
        // Ensure stops contains at least numStops elements.
        mYAxis.mEntries = new float[n];
        int i;
        float f;
        for (f = min, i = 0; i < n; f += interval, i++) {
            mYAxis.mEntries[i] = f;
        }

    }

    /*y轴显示小数标准*/
    protected String convertTitle(double average, double value) {
        String roundValue="";
        if (mYAxis.getAxisDependency() == YAxis.AxisDependency.LEFT) {//左侧y
            if (average < 0.002) {
                roundValue = String.format("%.4f", value);
            } else if (average < 0.02) {
                roundValue = String.format("%.3f", value);
            } else if (average < 0.2) {
                roundValue = String.format("%.2f", value);
            } else if (average < 2) {
                roundValue = String.format("%.1f", value);
            } else {
                roundValue = String.format("%.0f", value);
            }
            return roundValue;

        } else { //右侧y
            average=average*100;
            if (average < 0.01) {
                roundValue = String.format("%.3f", value * 100);
            } else if (average < 0.1) {
                roundValue = String.format("%.2f", value * 100);
            } else if (average < 1) {
                roundValue = String.format("%.1f", value * 100);
            } else {
                roundValue = String.format("%.0f", value * 100);
            }
            return roundValue+"%";
        }

    }


    protected void drawYLabels(Canvas c, float fixedPosition, float[] positions, float offset) {


        if (!TextUtils.isEmpty(mYAxis.getMintext()) && mYAxis.isShowOnlyMinMaxEnabled()) {
            // draw
            for (int i = 0; i < mYAxis.mEntryCount; i++) {

                String text = mYAxis.getFormattedLabel(i);

                if (i == 0) {
                    text = mYAxis.getMintext();
                }

                if (mYAxis.getTextColors() != null) {
                    mAxisLabelPaint.setColor(mYAxis.getTextColors()[i % mYAxis.getTextColors().length]);
                }

                if (i == 1) {
                    c.drawText(text, fixedPosition, mViewPortHandler.contentTop() + offset * 2.5f + 3, mAxisLabelPaint);
                } else if (i == 0) {
                    c.drawText(text, fixedPosition, mViewPortHandler.contentBottom() - 3, mAxisLabelPaint);
                }
            }
        } else {
/**
 *自定义y labels
 * */
            if (mYAxis.getYlabelStatic()) {
                float cha = (maxValue - minValue) / (mYAxis.getLabelCount() - 1);

                for (int i = 0; i < mYAxis.getLabelCount(); i++) {
//                    String text = mYAxis.getValueFormatter().getFormattedValue(cha * i + minValue) + "";
                    String text = convertTitle(cha, cha * i + minValue);
                    int labelHeight = Utils.calcTextHeight(mAxisLabelPaint, text);
                    if (mYAxis.getTextColors() != null) {
                        mAxisLabelPaint.setColor(mYAxis.getTextColors()[i % mYAxis.getTextColors().length]);
                    }
                    if (!mYAxis.isDrawTopYLabelEntryEnabled() && i >= mYAxis.mEntryCount - 1)
                        return;
                    if (i == mYAxis.getLabelCount() - 1)
                        c.drawText(text, fixedPosition, mViewPortHandler.contentTop() + offset * 2.5f + 3, mAxisLabelPaint);
                    else if (i == 0)
                        c.drawText(text, fixedPosition, mViewPortHandler.contentBottom() - 3, mAxisLabelPaint);
                    else
                        c.drawText(text, fixedPosition, mViewPortHandler.contentBottom() - 3 + labelHeight / 2 + (mViewPortHandler.contentTop() - mViewPortHandler.contentBottom()) / (mYAxis.getLabelCount() - 1) * i, mAxisLabelPaint);
                }
            } else {
                for (int i = 0; i < mYAxis.mEntryCount; i++) {

                    String text = mYAxis.getFormattedLabel(i);
                    if (!mYAxis.isDrawTopYLabelEntryEnabled() && i >= mYAxis.mEntryCount - 1)
                        return;

                    int labelHeight = Utils.calcTextHeight(mAxisLabelPaint, text);
                    float pos = positions[i * 2 + 1] + offset;

                    if ((pos - labelHeight) < mViewPortHandler.contentTop()) {

                        pos = mViewPortHandler.contentTop() + offset * 2.5f + 3;
                    } else if ((pos + labelHeight / 2) > mViewPortHandler.contentBottom()) {
                        pos = mViewPortHandler.contentBottom() - 3;
                    }
                    if (mYAxis.getTextColors() != null) {
                        mAxisLabelPaint.setColor(mYAxis.getTextColors()[i % mYAxis.getTextColors().length]);
                    }

                    c.drawText(text, fixedPosition, pos, mAxisLabelPaint);
                }
            }

        }


    }


}